// const DotenvWebpackPlugin = require('dotenv-webpack');
// const { config: configDotEnv } = require('dotenv');

const { loadEnvConfig } = require('@next/env');
const path = require('path');
const FilterWarningsPlugin = require('webpack-filter-warnings-plugin');

loadEnvConfig(
  path.join(__dirname, 'env'),
  process.env.RUN_ENV === 'development'
);
const { DefinePlugin, ProvidePlugin, IgnorePlugin } = require('webpack');
const { CONFIG_FILES } = require('next/dist/shared/lib/constants');

new IgnorePlugin({
  resourceRegExp: /\/node_modules\/(react-native|react-native-web)/,
  contextRegExp: /\/node_modules\/(react-native|react-native-web)/,
});

// function getCompactConfig(config) {
//   return Object.fromEntries(
//     Object.entries(config).filter(([key]) => !/mysql|secret/i.test(key))
//   );
// }

/** @type {import('next').NextConfig} */
const nextConfig = {
  reactStrictMode: true,
  swcMinify: true,
  // react 18 new feature, mounted will call twice https://legacy.reactjs.org/docs/strict-mode.html#ensuring-reusable-state
  reactStrictMode: false,
  experimental: {
    // appDir: true,
    // serverComponentsExternalPackages: ['react', 'react-dom'],
    // serverMinification: false,
  },

  // sassOptions: {
  //   includePaths: [
  //     path.join(__dirname, './src/styles'),
  //     path.join(__dirname, './src/views'),
  //   ],
  // },
  webpack: (config, { isServer, webpack, defaultLoaders, dev }) => {
    // if (!isServer) {
    // }

    // config.plugins.push(new DotenvWebpackPlugin({ path: "./.env" }));
    // config.plugins.push(
    //   // new ProvidePlugin({
    //   //   process: "global.process",
    //   // })
    //   new webpack.DefinePlugin({
    //     DOTENV: `(${JSON.stringify(getCompactConfig(dotEnvConfig))})`,
    //   })
    // );

    // config.plugins.push(
    //   new DotenvWebpackPlugin({
    //     path: `./env/.env.${process.env.NODE_ENV}`,
    //     prefix: 'import.meta.env.',
    //   })
    // );

    config.plugins.push(
      new FilterWarningsPlugin({
        exclude: /the request of a dependency is an expression/g,
      })
    );

    if (process.env.NEXT_PUBLIC_APP_ENV === 'development') {
      // config.devtool = 'cheap-source-map';
      config.output = config.output || {};
      config.output.pathinfo = true;

      // config.mode = 'none'; // not sure
      config.optimization.minimize = false;
    }

    // config.plugins.push(
    //   new webpack.NormalModuleReplacementPlugin(/node:/, resource => {
    //     resource.request = resource.request.replace(/^node:/, '');
    //   })
    // );

    // config.resolve = config.resolve || {};
    // config.resolve.alias = config.resolve.alias || {};
    // Object.assign(config.resolve.alias, {
    //   'node:async_hooks': 'async_hooks',
    //   'node:buffer': 'buffer',
    // });

    // config.externals = {
    //   knex: 'commonjs knex',
    // };

    config.externals = config.externals || {};
    if (Array.isArray(config.externals)) {
      config.externals.push('knex');
    } else {
      Object.assign(config.externals, {
        knex: 'commonjs knex',
      });
    }

    config.module = config.module || {};
    config.module.rules = config.module.rules || [];
    config.module.rules.push({
      test: /\.svg$/,
      include: path.resolve(__dirname, 'src/assets/icons'),
      use: {
        loader: 'svg-sprite-loader',
        options: {
          symbolId: 'icon-[name]',
        },
      },
    });

    return config;
  },

  images: {
    remotePatterns: [
      {
        hostname: '**',
      },
    ],
    deviceSizes: [640, 750, 1080, 1300, 1920],
  },

  async rewrites() {
    return [
      {
        source: '/v2/:path*',
        destination: `https://api.dropshipzone.com.au/v2/:path*`,
      },
    ];
  },
};

module.exports = nextConfig;
